angular.module('App.services', [])
.constant('ApiEndpoint', {
	url : "../"
})
.factory('settings', ['$rootScope', 'Page', 'ApiEndpoint', function($rootScope, Page, ApiEndpoint) {
	
	var settings = {};
	
	var __local = window.location;
	var __contextPath = __local.pathname.split("/")[1];
	var __basePath = __local.protocol+"//"+__local.host+"/"+__contextPath;
	settings.basePath = __basePath;
	
	 $.ajax({
         url : ApiEndpoint.url + 'www/setting.do',
         method: 'POST',
         async: false,
         success : function(resp){
        	 if(resp.resultCode == '1'){
 		        if(resp.data){
 		        	settings = angular.extend(resp.data,settings);
 		        }
             }
         }
     });
	
	
	$rootScope.settings = settings;
    return settings;
}])
.factory('localStore',['$window',function($window){
	return{        //存储单个属性
		set :function(key,value){
		  $window.localStorage[key]=value;
		},        //读取单个属性
		get:function(key,defaultValue){
		  return  $window.localStorage[key] || defaultValue;
		},        //存储对象，以JSON格式存储
		setObject:function(key,value){
		  $window.localStorage[key]=JSON.stringify(value);
		},        //读取对象
		getObject: function (key) {
		  return JSON.parse($window.localStorage[key] || '{}');
		}
     };
 }])
 .factory('sessionStore',['$window',function($window){
	return{        //存储单个属性
		set :function(key,value){
		  $window.sessionStorage[key]=value;
		},        //读取单个属性
		get:function(key,defaultValue){
		  return  $window.sessionStorage[key] || defaultValue;
		},        //存储对象，以JSON格式存储
		setObject:function(key,value){
		  $window.sessionStorage[key]=JSON.stringify(value);
		},        //读取对象
		getObject: function (key) {
		  return JSON.parse($window.sessionStorage[key] || '{}');
		}
     };
 }])
.service('Page', function($rootScope, $http, $state,ApiEndpoint) { // injectables go here
	var self = this; // Save reference
	/*
	this.alert = function(message, yesCallback) {
	    layer.open({
	        content : message,
	        btn : ['OK'],
	        yes : function(index) {
	            if (yesCallback) {
	                layer.close(index)
	                yesCallback();
	            }
	        }
	    });
	};

	this.notice = function(message) {
	    layer.open({
	        content : message,
	        time : 2
	    });
	};

	this.confirm = function(message, yesCallback) {
	    layer.open({
	        content : message,
	        btn : ['确认', '取消'],
	        shadeClose : false,
	        anim : false,
	        yes : function(index) {
	            if (yesCallback) {
	                layer.close(index)
	                yesCallback();
	            }
	        },
	        no : function() {
	        }
	    });
	};

	this.ajax = function(method, url, data, isFilter, callback, errorCallback) {
	    // 加上时间戳，避免浏览器缓存
	    url = url += (url.indexOf('?') > 0 ? '&' : '?') + '_t=' + (new Date()).valueOf();
	    var loadi = null;
	    if (isFilter) {
	        loadi = layer.open({
	            type : 2,
	            content : ''
	        });
	    }
	    data = data || {};
	    if ($rootScope.user) {
	        data.sid = $rootScope.user.token;
	    }
	    $http({
	        method : method,
	        url : url,
	        data : data,
	        cache : false
	    }).success(function(response) {
	        if (loadi != null) {
	            layer.close(loadi);
	        }
	        if (response.resultCode == '1') { //成功
	            if (callback) {
	                callback(response);
	            }
	        } else if (errorCallback) {
	            errorCallback(response)
	        } else if (response.resultCode) {
	            self.notice(response.resultDes);
	        }
	    });
	};*/

	this.alert = function(message, okCallback) {
		layer.alert(message, function(index) {
			if (okCallback) {
				okCallback();
			}
			layer.close(index);
		});
	};
	this.notice = function(message) {
		layer.msg(message);
	};
	this.confirm = function(message, okCallback) {
		layer.confirm(message, {
			icon : 3,
			title : "提示"
		}, function(index) {
			if (okCallback) {
				okCallback();
			}
			layer.close(index);
		});
	};
	this.ajax = function(method, url, data, isFilter, callback, errorCallback) {
		url = url += (url.indexOf("?") > 0 ? "&" : "?") + "_t=" + new Date().valueOf();
		var loadi = null;
		if (isFilter) {
			loadi = layer.load(0);
		}
		$http({
			method : method,
			url : (url.indexOf('http') == 0 ? '' : ApiEndpoint.url) + url,
			data : data,
			cache : false
		}).success(function(response) {
			if (loadi != null) {
				layer.close(loadi);
			}
			if (response.resultCode == "1" || url.indexOf("service/") == 0) {
				if (callback) {
					callback(response);
				}
			} else if (errorCallback) {
				errorCallback(response);
			} else if (response.resultCode) {
				self.notice(response.resultDes);
			}
		}).error(function(response) {
			if (loadi != null) {
				layer.close(loadi);
			}
		});
	};

	this.ajaxPost = function(url, data, callback, errorCallback) {
		this.ajax('POST', url, data, true, callback, errorCallback);
	};

	this.ajaxPostQuiet = function(url, data, callback, errorCallback) {
		this.ajax('POST', url, data, false, callback, errorCallback);
	};

	this.ajaxGet = function(url, data, callback, errorCallback) {
		this.ajax('GET', url, data, true, callback, errorCallback);
	};

	this.ajaxGetQuiet = function(url, data, callback, errorCallback) {
		this.ajax('GET', url, data, false, callback, errorCallback);
	};

	this.ajaxPut = function(url, data, callback, errorCallback) {
		if (!data) {
			data = {};
		}
		data._method = 'PUT';
		this.ajaxPost(url, data, callback, errorCallback);
	},

	this.ajaxDelete = function(url, data, callback, errorCallback) {
		if (!data) {
			data = {};
		}
		data._method = 'DELETE';
		this.ajaxPost(url, data, callback, errorCallback);
	}

});